<template>
  <div>
    <fragment>
      <el-container>
        <el-aside width="collapse">
          <navbar :menus="menus" :collapse="isCollapse"></navbar>
        </el-aside>
        <el-container>
          <el-header>
            <i
              class="el-collapse el-icon-s-fold"
              :class="isCollapse ? 'collapseTransform' : ''"
              @click="handleCollapse"
            ></i>

            <el-dropdown>
              <span class="el-dropdown-link">
                {{ username }}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>用户信息</el-dropdown-item>
                <el-dropdown-item>修改密码</el-dropdown-item>
                <el-dropdown-item @click.native="handleOut"
                  >注销</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </el-header>
          <el-main>
            <router-view></router-view>
          </el-main>
          <el-footer>
            Copyright © 2021 - 2021 9I Co. All Rights Reserved. 就爱科技
            版权所有
          </el-footer>
        </el-container>
      </el-container>
    </fragment>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确定退出登入？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleLogout">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import routes from "../router/routes";
import Navbar from "../components/Navbar";

import jwtDecode from "jwt-decode";
import { getToken } from '../untils/auth'

export default {
  components: {
    Navbar,
  },
  data() {
    return {
      isCollapse: false,
      dialogVisible: false,
      username: "",
    };
  },
  mounted() {
    let token = getToken()
    
    console.log(token);
    if(token){
   this.username = jwtDecode(token).username;
    console.log(this.username);
    }
   
  },
  computed: {
    menus() {
      let arr = this.resolve(routes);
      console.log(arr);

      return arr;
    },
  },
  methods: {
    resolve(arr, parentPath = "") {
      let newArr = [];
      arr.forEach((route) => {
        let obj = Object.assign({}, route);
        parentPath =
          parentPath && parentPath.lastIndexOf("/") !== parentPath.length - 1
            ? parentPath + "/"
            : parentPath;

        obj.path = parentPath + obj.path;
        if (obj.children && obj.children.length > 0) {
          obj.children = this.resolve(obj.children, obj.path);
        }
        if (obj.children && obj.children.length === 1) {
          let oldtitle = obj.meta.title;
          obj = obj.children[0];
          obj.meta.title = oldtitle;
        }
        if (!obj.hiden) {
          newArr.push(obj);
        }
      });
      return newArr;
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    handleLogout() {
      this.dialogVisible = false;
      localStorage.removeItem("accessToken");
      localStorage.removeItem("refreshToken");

      this.$router.push("/login");
    },
    handleOut() {
      this.dialogVisible = true;
    },
  },
};
</script>

<style scoped>
.el-header {
  background-color: #eeeeee;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
/* .el-aside {
  width: 200px !important;
} */
.el-main {
  height: calc(100vh - 120px);
  overflow-y: scroll;
}
.el-footer {
  background-color: #eeeeee;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}
.el-collapse {
  font-size: 2rem;
}
.collapseTransform {
  transform: rotate(-90deg);
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>